package priv.mill.net;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.List;

public class MyDecoder extends ByteToMessageDecoder {

	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in,
			List<Object> out) throws Exception {
		byte[] dst = new byte[in.readableBytes()];
		in.readBytes(dst);
		MyHttp info = fromBytes(dst);
		out.add(info);
	}

	private MyHttp fromBytes(byte[] dst) throws Exception {
		try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				dst);
				ObjectInputStream inputStream = new ObjectInputStream(
						byteArrayInputStream);) {

			return (MyHttp) inputStream.readObject();
		}

	}
}
